//
//  isLogin.m
//  jandaobao
//
//  Created by apple on 15/6/1.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "isLogin.h"


@implementation isLogin
+(BOOL)isLoginTo{
    NSString *userID=[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    if (![userID intValue]>0) {
        
        return NO;
    }else{
        return YES;
    
    }

}

/*************
 *获取用户的id,
 ****************/
+(NSString *) getCustomerId{
    NSString * customerId =  [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    if(customerId==nil) return @"0";
    return customerId;
}

/*************
 *获取 城市id,
 ****************/
+ (NSString *)getCityId{
    
    NSString * customerId =  [[NSUserDefaults standardUserDefaults]objectForKey:@"provinceName"];
    if(customerId==nil) return @"0";
    return customerId;

}
/*************
 *获取 城市名,
 ****************/
+ (NSString *)getCityName{
    
    NSString * customerId =  [[NSUserDefaults standardUserDefaults]objectForKey:@"provinceID"];
    if(customerId==nil) return @"0";
    return customerId;
    
}

/*************
 *获取 城市经度,
 ****************/
+ (NSString *)getlatitude{
    
    NSString * customerId =  [[NSUserDefaults standardUserDefaults]objectForKey:@"latitude"];
    if(customerId==nil) return @"0";
    return customerId;
    
}
/*************
 *获取 城市经度,
 ****************/
+ (NSString *)getlongitude{
    
    NSString * customerId =  [[NSUserDefaults standardUserDefaults]objectForKey:@"longitude"];
    if(customerId==nil) return @"0";
    return customerId;
    
}


+(void)setVendorImageUrl:(UIImageView *)image:(NSString *)imgPath {

    
    [image setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",imgPath]] placeholderImage:[UIImage imageNamed:@"empty_photo"]];
}



+(void)Fillet:(UIButton *)btn:(int)windth{

    btn.layer.cornerRadius =windth / 2;
    btn.clipsToBounds = YES;
    btn.layer.masksToBounds = YES;
    btn.layer.borderColor = [UIColor whiteColor].CGColor;
    btn.layer.borderWidth = 2.f;


}

+(void)FilletImg:(UIImageView *)btn:(int)windth{
    
    btn.layer.cornerRadius =windth / 2;
    btn.clipsToBounds = YES;
    btn.layer.masksToBounds = YES;
    btn.layer.borderColor = [UIColor whiteColor].CGColor;
    btn.layer.borderWidth = 2.f;
    
    
}

+(void)FilletView:(UIView *)viewTwo:(int)windth{

    viewTwo.layer.cornerRadius =windth;
    viewTwo.clipsToBounds = YES;
    viewTwo.layer.masksToBounds = YES;
    viewTwo.layer.borderColor = [UIColor whiteColor].CGColor;
    viewTwo.layer.borderWidth = 2.f;

}

+(void)CuttingAngle:(UIButton *)btn:(int)windth{
    
    btn.layer.cornerRadius =windth ;
    btn.clipsToBounds = YES;
    btn.layer.masksToBounds = YES;
    btn.layer.borderColor = [UIColor whiteColor].CGColor;
    btn.layer.borderWidth = 2.f;
    
}

+ (void)loginout
{
    if (SESSION_ID) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"categoryID"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"parentID"];
        //城市的ID和Name不能去清除了知道么.
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"province_id"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"province_Name"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userName"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
        
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userID"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"categoryName"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"prentID"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        
    }


}


+ (NSString *)getCachPath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    
    return path;
}






+ (NSString *)contentToHtml:(float )fontSize :(NSString *)fontFamily :(NSString *)fontColor :(NSString *)contentValue
{
    contentValue = [contentValue stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];
    contentValue = [contentValue stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    contentValue = [contentValue stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    contentValue = [contentValue stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\r\n"];
    contentValue = [contentValue stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    contentValue = [contentValue stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    contentValue = [contentValue stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    contentValue = [contentValue stringByReplacingOccurrencesOfString:@"&#160;" withString:@" "];
    
    
    return [NSString stringWithFormat:@"<!DOCTYPE html>"
            "<html lang=\"\">"
            "<meta charset=\"utf-8\">"
//            "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\"> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no\">"
            "<link rel=\"stylesheet\" type=\"text/css\" href=\"%@css/public.css\"/> <base href=\"%@\"/>"
//            "<style type=\"text/css\"> \n"
//            "body {max-width:100%%;font-size: %fpx; font-family: \"%@\"; color: %@;}\n"
//            "img{max-width:200%%;width:200%%;}\n"
          //  "</style> \n"
            "</head> \n"
            "<body>%@</body> \n"
            "</html>",IMGPATH, IMGPATH,contentValue];
}



+ (NSString *)isHttpHeader:(NSString *)photoUrl
{
    if (![photoUrl isEqual:[NSNull null]] ) {
        if ([photoUrl hasPrefix:@"http"]) {
            return photoUrl;
        }else
        {
            return PATH(photoUrl);
        }

    }
    else
    {
        return photoUrl;
    }

}



/**
 *  修正微博的发送时间
 */


+ (NSString *)isSubBirth:(NSString *)sourceString
{
    if ([sourceString isEqual:[NSNull null]] || [[NSString stringWithFormat:@"%@",sourceString] isEqualToString:@"<null>"]) {
        
        return @"";
    }else
    {
        if (sourceString.length>9) {
            return [sourceString substringWithRange:NSMakeRange(0, 10)];
            
        }else
        {
            return sourceString;
        
        }
        
    }


}


+ (NSString *)isNull:(NSString *)sourceString
{
    if ([sourceString isEqual:[NSNull null]] || [[NSString stringWithFormat:@"%@",sourceString] isEqualToString:@"<null>"]) {
        
        return @" ";
    }else
    {
        
        return [NSString stringWithFormat:@"%@",sourceString];
    }


}

+ (NSString *)created_at:(NSString *)_created_at
{
    // 获取微博发送时间 Fri May 09 16:30:34 +0800 2014
    
    if(_created_at.length<19)
    {
         return _created_at;
        }else
        {

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [formatter setLocale:locale];
    
    formatter.dateFormat = @"yyyyMMddHHmmss";
    
    NSMutableString *str=[[NSMutableString alloc] init];
    [str appendString:[_created_at substringWithRange:NSMakeRange(0, 4)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(5, 2)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(8, 2)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(11, 2)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(14, 2)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(17, 2)]];
    
    
    NSDate *createdDate = [formatter dateFromString:str];
    
    // 判断
    if (createdDate.isToday) { // 今天以内
        if (createdDate.deltaToNow.hour >= 1) { // 1个小时以前
            return [NSString stringWithFormat:@"%ld小时前", (long)createdDate.deltaToNow.hour];
        } else if (createdDate.deltaToNow.minute >= 1) { // 1分钟以前
            return [NSString stringWithFormat:@"%ld分钟前", (long)createdDate.deltaToNow.minute];
        } else { // 1分钟以内
            return @"刚刚";
        }
    } else if (createdDate.isYesterday) { // 昨天以前
        formatter.dateFormat = @"昨天 HH:mm";
        return [formatter stringFromDate:createdDate];
    } else if (createdDate.isThisYear) { // 今年以内
        formatter.dateFormat = @"MM-dd HH:mm";
        return [formatter stringFromDate:createdDate];
    } else { // 去年以前
        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
        return [formatter stringFromDate:createdDate];
    }
    
    }
}


+ (BOOL)isOneDay:(NSString *)_created_at
{
    
    
    if(_created_at.length<18)
    {
        return NO;
    }else
    {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    [formatter setLocale:locale];
    
    formatter.dateFormat = @"yyyyMMddHHmmss";
    
    NSMutableString *str=[[NSMutableString alloc] init];
    [str appendString:[_created_at substringWithRange:NSMakeRange(0, 4)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(5, 2)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(8, 2)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(11, 2)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(14, 2)]];
    [str appendString:[_created_at substringWithRange:NSMakeRange(17, 2)]];
    
    
    NSDate *createdDate = [formatter dateFromString:str];
    
    // 判断
    if (createdDate.isToday) { // 今天以内
        return YES;
    }else
    {
        return NO;
        
    }
    }
    
}


+ (CGFloat)GetFontHeight:(NSString *)string font:(UIFont *)font width:(float)width{
    
    if ([string isKindOfClass:[NSNull class]] || !string || [[NSString stringWithFormat:@"%@",string] isEqualToString:@"<null>"]) {
        return 0;
    }else{
        if (font == nil)
        {
        font = [UIFont systemFontOfSize:15.f];
        }
        CGSize textSize = [string boundingRectWithSize:CGSizeMake(width, MAXFLOAT)options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font}context:nil].size;
        
    return textSize.height ;
    }
    
}


+ (NSString *)getAge:(NSString *)dateStr
{
    
    
    if (![dateStr isEqual:[NSNull null]]) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
        [formatter setLocale:locale];
        
        
        formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        NSDate *birthDate=[formatter dateFromString:dateStr];
        
        NSTimeInterval dateDiff = [birthDate timeIntervalSinceNow];
        int age=trunc(dateDiff/(60*60*24))/365;
        return [NSString stringWithFormat:@"%d",ABS(age)];

    }else{
        return @"未设置";
    }
    
}

+(NSString*) uuid {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);
    return result;
}


+ (NSMutableAttributedString *)rowHeight:(NSString *)sourceString
{

    NSMutableAttributedString *attributedString2 = [[NSMutableAttributedString alloc] initWithString:[isLogin formatString:[isLogin isNull:sourceString]]];
    NSMutableParagraphStyle *paragraphStyle2 = [[NSMutableParagraphStyle alloc] init];
    
    [paragraphStyle2 setLineSpacing:4];//调整行间距
    
    [attributedString2 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle2 range:NSMakeRange(0,[[isLogin formatString:[isLogin isNull:sourceString]] length] )];
    return  attributedString2;
    


}

+ (NSString *)formatString:(NSString *)sourceString
{
    
    if ([sourceString isEqual:[NSNull null]] || [[NSString stringWithFormat:@"%@",sourceString] isEqualToString:@"<null>"]) {
        return @"";
    }
    
    sourceString = [sourceString stringByReplacingOccurrencesOfString:@"&#8203;" withString:@""];
    
    
    sourceString = [sourceString stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@" "];
    sourceString = [sourceString stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    sourceString = [sourceString stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    sourceString = [sourceString stringByReplacingOccurrencesOfString:@"\r\n" withString:@"\r\n"];
    sourceString = [sourceString stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    sourceString = [sourceString stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    sourceString = [sourceString stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    sourceString= [sourceString stringByReplacingOccurrencesOfString:@"&#160;" withString:@" "];
    
    sourceString= [sourceString stringByReplacingOccurrencesOfString:@"<null>" withString:@""];
    
    sourceString= [sourceString stringByReplacingOccurrencesOfString:@"&ldquo;" withString:@"'"];
    sourceString= [sourceString stringByReplacingOccurrencesOfString:@"&rdquo;" withString:@"'"];
    
    return sourceString;
    


}


+ (NSString *)jumpToBizPay {
    
    //============================================================
    // V3&V4支付流程实现
    // 注意:参数配置请查看服务器端Demo
    // 更新时间：2015年11月20日
//    //============================================================
//    NSString *urlString   = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
//    //解析服务端返回json数据
//    NSError *error;
//    //加载一个NSURL对象
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
//    //将请求的url数据放到NSData对象中
//    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//    if ( response != nil) {
//        NSMutableDictionary *dict = NULL;
//        //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
//        dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
//        
//        NSLog(@"url:%@",urlString);
//        if(dict != nil){
//            NSMutableString *retcode = [dict objectForKey:@"retcode"];
//            if (retcode.intValue == 0){
//                NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
//                
                //调起微信支付
    return @"0";
                //日志输出
//                NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
//                return @"";
//            }else{
//                return [dict objectForKey:@"retmsg"];
//            }
//        }else{
//            return @"服务器返回错误，未获取到json对象";
//        }
//    }else{
//        return @"服务器返回错误";
//    }
}


/*
 *随机生成15位订单号,外部商户根据自己情况生成订单号
 */


+ (id)storeBordName:(NSString *)stordBordName
                   :(NSString *)storeBordID
{
    if (stordBordName && storeBordID) {
        
        UIStoryboard *mainStory = [UIStoryboard storyboardWithName:stordBordName bundle:nil];
        if (mainStory) {
            return [mainStory instantiateViewControllerWithIdentifier:storeBordID]!=nil?[mainStory instantiateViewControllerWithIdentifier:storeBordID] :nil;
        }else
        {
            return nil;
        }

    }else
    {
    
        return nil;
    }
    
    
    

    

}

@end
